<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Account;
use App\Connect;
use App\Line;
use App\Http\Controllers\UsersController as Users;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Rad_users;


class AccountController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    

    //连接账户信息

	public function get(Request $request)
	{

		if (empty($request->input('token'))){
			$return = array(
				'code' => -1,
				'msg' => '用户信息有误，请重新登录',
				'data' => array()
			);
			return response()->json($return);
		}
		$token = $request->input('token');
		$user_id = Users::getIdByWebToken($token);


		if (empty($request->input('line_id'))){
			$return = array(
				'code' => -1,
				'msg' => '线路信息有误',
				'data' => array()
			);
			return response()->json($return);
		}

		$line_id = $request->input('line_id');

		$line = Line::where('id','=',$line_id)->first();

		if (empty($line)){
			$return = array(
				'code' => -1,
				'msg' => '线路信息有误',
				'data' => array()
			);
			return response()->json($return);
		}

		$user = Rad_users::find($user_id);

		$account_name = $user->username;
		$account_password = $user->password;
		$account = array(
			'name' => $account_name,
			'password' => $account_password,
		);
		$return = array(
			'code' => 0,
			'msg' => 'OK',
			'data' => array(
				'account' => $account,
				'connect_time' => time()
			)
		);
		return response()->json($return);
	}

//    public function get(Request $request)
//    {
//    	if (empty($request->input('token'))){
//    		$return = array(
//    				'code' => -1,
//    				'msg' => '用户信息有误，请重新登录',
//    				'data' => array()
//    		);
//    		return response()->json($return);
//    	}
//    	$token = $request->input('token');
//    	$user_id = Users::getIdByToken($token);
//    	if (empty($request->input('line_id'))){
//    		$return = array(
//    				'code' => -1,
//    				'msg' => '线路信息有误',
//    				'data' => array()
//    		);
//    		return response()->json($return);
//    	}
//    	$line_id = $request->input('line_id');
//    	$line = Line::where('id','=',$line_id)->first();
//    	if (empty($line)){
//    		$return = array(
//    				'code' => -1,
//    				'msg' => '线路信息有误',
//    				'data' => array()
//    		);
//    		return response()->json($return);
//    	}
//    	$supplier_id = $line->supplier_id;
////     	$account = $this->getAccountByLine($line_id);
//
//    	//查询连接表当前是否存在使用中的线路账户
//    	$connect = Connect::where('supplier_id','=',$supplier_id)->where('token','=',$token)->first();
//    	if (!empty($connect)){
//    		//取出这一条计数不变
//    		$account = Account::orderBy('updated_at', 'desc')->find($connect->account_id);
//    		//更新当前连接次数
//    		$connect->times = $connect->times+1;
//    		$connect->save();
//    	}else {
//    		$date = date('Y-m-d H:i:s',time());
//    		//获取线路账户
//    		$account = Account::where('supplier_id',$supplier_id)->where('enable_flag',1)->whereColumn('terminal_total', '>', 'terminal_used')->where('terminal_total','>',0)->where('deadline','>',$date)->first();
//    		if (empty($account)){
//    			$return = array(
//    					'code' => -1,
//    					'msg' => '连接失败，请更换线路重试',
//    					'data' => array()
//    			);
//    			return response()->json($return);
//    		}
//    		//更新已使用数量 并记录连接在线表
//    		if ($account->terminal_used<$account->terminal_total){
//    			$account->terminal_used = $account->terminal_used+1;
//    			$account->save();
//    			$connect = new Connect();
//    			$connect->token = $token;
//    			$connect->account_id = $account->id;
//    			$connect->supplier_id = $account->supplier_id;
//    			$connect->user_id = $user_id;
//    			$connect->save();
//
//    		}else{
//    			$return = array(
//    					'code' => -1,
//    					'msg' => '连接失败，请重试',
//    					'data' => array()
//    			);
//    			return response()->json($return);
//    		}
//    	}
//
//    	$account_name = $account->name;
//    	$account_password = $account->password;
//    	$account = array(
//    			'name' => $account_name,
//    			'password' => $account_password,
//    	);
//    	$return = array(
//    			'code' => 0,
//    			'msg' => 'OK',
//    			'data' => array(
//    					'account' => $account,
//    					'connect_time' => time($connect->updated_at)
//    			)
//    	);
//        return response()->json($return);
//    }
    
    static public function getAccountByLine($line_id){
    	$account = array(
    			'name' => '123',
    			'password' => '123',
    	);
    	return $account;
    }
    
    
}